﻿package com.wandian.model
{
	import com.wandian.controller.CLZNoticeName;
	import com.wandian.data.GameData;
	import com.wandian.util.loader.CLZLoadSWF;
	import com.wandian.util.loader.SWFLoader;
	
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	
	import org.puremvc.as3.patterns.proxy.Proxy;
	/**   
	* @author long
	* @QQ 108232706
	* @version 1.0   
	*/   
	public class SwfDataProxy extends Proxy
	{
		private var _arrLoadermaterial:Array;
		private var _loading:SWFLoader;
		private var _xmlData:XMLDataProxy;
		private var _nNum:int=0;
		private var _arrLoader:Array;
		private var _singleLoadSWF:CLZLoadSWF;
		private var _arrSingleSWF:Array;
		public static const NAME:String="SwfDataProxy";

		public function SwfDataProxy(param1:Object=null)
		{
			this._arrLoadermaterial=[];
			this._arrSingleSWF=[];
			super(NAME, param1);
			this._arrLoadermaterial=[];
			this._loading=new SWFLoader();
			this._loading.addEventListener(Event.COMPLETE, this.completeLoaderHandler);
			this._loading.addEventListener(ProgressEvent.PROGRESS, this.progressLoaderHandler);
			this._loading.addEventListener(IOErrorEvent.IO_ERROR, this.onError);
			return;
		} 

		public function load():void
		{
			this._arrLoader=GameData.arrSwfUrl;//=[GameData.flashUrl + GameData.swf["assets"] + GameData.flashver["assets"] + ".swf", GameData.flashUrl + GameData.swf["windows"] + GameData.flashver["windows"] + ".swf", GameData.flashUrl + GameData.swf["top"] + GameData.flashver["top"] + ".swf", GameData.flashUrl + GameData.swf["table"] + GameData.flashver["table"] + ".swf", GameData.flashUrl + GameData.swf["game"] + GameData.flashver["game"] + ".swf", GameData.flashUrl + GameData.swf["rank"] + GameData.flashver["rank"] + ".swf"];
			this.loadSWF();
			return;
		} 

		private function loadSWF():void
		{
//			this._loading.Load("http://192.168.1.103/texas/"+this._arrLoader[this._nNum]);
			this._loading.Load(this._arrLoader[this._nNum]);
			return;
		} 

		private function completeLoaderHandler(event:Event):void
		{
			var _loc_2:CLZLoadSWF=null;
			GameData.domainArr.push(event.target.loadinfo.applicationDomain);
			this._nNum+=1;
			if (this._nNum >= this._arrLoader.length)
			{
//				this.sendNotification(CLZNoticeName.LOAD_SWF_COMPLETE);
				this.sendNotification(CLZNoticeName.APPLICATIONMEDIATOR_LOAD_SWF_COMPLETE);
				//----若防止过大，分开下
//				_loc_2=new CLZLoadSWF();
//				_loc_2.Load("view/ui/sound.swf");
			}
			else
			{
				this.loadSWF();
			}
			return;
		} 

		private function onError(event:IOErrorEvent):void
		{
			return;
		} 

		private function progressLoaderHandler(event:ProgressEvent):void
		{
			var _loc_2:int=int((event.bytesLoaded * 100 / event.bytesTotal).toFixed(1));
			var _loc_3:*=100 / this._arrLoader.length * (_loc_2 / 100) + this._nNum * (100 / this._arrLoader.length);
			this.sendNotification(CLZNoticeName.LOAD_SWF_PRO, int(_loc_3.toFixed(0)));
			return;
		} 

		public function singleLoadSWF(loadName:String, noticationName:String="", otherType:int=0):void
		{
			var completeSingLoaderHandler:Function;
			var progressSingLoaderHandler:Function;
			var strUrl:String;
			
			completeSingLoaderHandler=function(event:Event):void
			{
				_arrSingleSWF.push(loadName);
				if (noticationName != "")
				{
					facade.sendNotification(noticationName, otherType);
				}
				if (noticationName == CLZNoticeName.LOADING_LOAD_SWF_COMPLETE)
				{
					return;
				}
				facade.sendNotification(CLZNoticeName.LOAD_SINGSWF_COMPLETE);
				return;
			} 
			;
			progressSingLoaderHandler=function(event:ProgressEvent):void
			{
				if (noticationName != CLZNoticeName.LOADING_LOAD_SWF_COMPLETE)
				{
				}
				if (noticationName == "")
				{
					return;
				}
				var _loc_2:*=int((event.bytesLoaded * 100 / event.bytesTotal).toFixed(1));
				facade.sendNotification(CLZNoticeName.LOAD_SINGSWF_PRO, _loc_2);
				return;
			} 
			;
			var isLoad:Boolean;
			var i:int;
			while (i < this._arrSingleSWF.length)
			{

				if (this._arrSingleSWF[i] == loadName)
				{
					isLoad;
					break;
				}
				i=(i + 1);
			}
			if (!isLoad)
			{
				if (otherType)
				{
					strUrl=GameData.flashUrl + "assets/swf/" + otherType + GameData.flashver[otherType] + ".swf";
				}
				else
				{
					strUrl=GameData.flashUrl + GameData.swf[loadName];// + GameData.flashver[loadName] + ".swf";
				}
				this._singleLoadSWF=new CLZLoadSWF();
				this._singleLoadSWF.addEventListener(Event.COMPLETE, completeSingLoaderHandler);
				this._singleLoadSWF.addEventListener(ProgressEvent.PROGRESS, progressSingLoaderHandler);
				this._singleLoadSWF.Load(strUrl);
			}
			else if (noticationName != "")
			{
				facade.sendNotification(noticationName, otherType);
			}
			return;
		} 

	}
}
